package control
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import models.ProfileModel;
	

	public class Control extends flash.events.EventDispatcher
	{
		public function Control() {}
		
		private const timerNum:int=1;
		private var gameTimer:Timer;

		public function setRunVec(value:IRunCtrl):void
		{
			ProfileModel.instant._runVec.push(value);
		}

		
		public function initGameCtrl(run:Boolean=true):void{
			trace("ppp")
			if(run){
				trace("ppp2")
				gameTimer=new Timer(timerNum);
				gameTimer.addEventListener(TimerEvent.TIMER,runGame);
				gameTimer.start();
			}else{
				gameTimer.stop();
				gameTimer.removeEventListener(TimerEvent.TIMER,runGame);
			}	
		}
		
		private function runGame(evt:TimerEvent):void
		{
//			trace(ProfileModel.instant._runVec.length);
			if(ProfileModel.instant._runVec.length>0){
				for (var j:int = ProfileModel.instant._runVec.length-1; j >=0; j--) 
				{
					if(ProfileModel.instant._runVec[j]){
						ProfileModel.instant._runVec[j].Run(j);
					}
				}
			}		
		}
		
		
		//*****************************************************
		// Singleton 
		//*****************************************************
		private static var _instant:Control;
		public static function get instant():Control
		{
			if(null == _instant)
			{
				_instant =  new Control();
			}
			return _instant;
		}
	}
}